/**
 * @Copyright © 2015 广州四三九九信息科技有限公司. All Rights Reserved.
*/

package com.jkdroid.maya.log;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 记录到sp的日志策略
 * @author guanzhihao
 * @date   2015年10月31日
 *
 */
public abstract class SpStrategy extends AbstractEventLogStrategy<JSONObject>{

	private static final String TAG = "event";
	private static final String EVENT_LOG_URL = "http://dpdcs.4399sy.com/event.php";
	private static String EVENT_LOG_FILE = "event_log_base";
	private static String EVENT_LOG_FIELD = "events";
	private static String EVENT_LOG_SEND = "event_sending";

	public ArrayList<JSONObject> getEventLogs(Context context) {
		String readEventLog = readEventLog(context);
		ArrayList<JSONObject> arr = new ArrayList<JSONObject>();
		strToArr(arr, readEventLog);
		return arr;
	}

	public JSONObject createEvent(String eventId, String eventName, String eventValue) {
		if(TextUtils.isEmpty(eventId)){
			return null;
		}
		JSONObject eventJson = new JSONObject();
		if (!TextUtils.isEmpty(eventId)) {
			try {
				eventJson.put("id", eventId);
				eventJson.put("n", eventName);
				eventJson.put("v", eventValue);
				eventJson.put("t", "" + (System.currentTimeMillis() / 1000));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return eventJson;
	}

	@Override
	public void backupEventlogs(Context context) {
		backupLog(context);
	}

	@Override
	public ArrayList<JSONObject> getBackupEventLogs(Context context) {
		ArrayList<JSONObject> arrayList = new ArrayList<JSONObject>();
		strToArr(arrayList , getBackupLog(context));
		return arrayList;
	}

	@Override
	public void saveEventLogs(Context context, ArrayList<JSONObject> records) {
		saveEventLog(context, arrToStr(records));
	}
	
	private static String arrToStr(ArrayList<JSONObject> list) {
		JSONArray eventArray = new JSONArray();
		for (int i = 0; i < list.size(); i++) {
			eventArray.put(list.get(i));
		}
		return eventArray.toString();
	}

	private static boolean strToArr(ArrayList<JSONObject> arr, String str) {
		boolean ret = true;
		try {
			JSONArray oldJson = new JSONArray(str);
			for (int i = 0; i < oldJson.length(); i++) {
				arr.add((JSONObject) oldJson.get(i));
			}
		} catch (Exception e) {
//			e.printStackTrace();
			ret = false;
		}
		return ret;
	}
	
	// 清除保存到文件中的event 事件
	@Override
	public void clearEventLogs(Context context) {
		saveEventLog(context, "");
	}

	@Override
	public void clearBackupEventLogs(Context context){
		getSp(context).edit().putString(EVENT_LOG_SEND, "").commit();
	}
	
	// 获取保存到文件中的event 事件，组装成一个json格式的数据串
	public String readEventLog(Context context) {
		return getSp(context).getString(EVENT_LOG_FIELD, "");
	}
	
	public void saveEventLog(Context context, String str){
		getSp(context).edit().putString(EVENT_LOG_FIELD, str).apply();
	}

	private SharedPreferences getSp(Context context){
		return context.getSharedPreferences(EVENT_LOG_FILE, Context.MODE_PRIVATE);
	}
	
	public void backupLog(Context context){
		getSp(context).edit().putString(EVENT_LOG_SEND, readEventLog(context)).commit();
	}
	
	public String getBackupLog(Context context){
		return getSp(context).getString(EVENT_LOG_SEND, "");
	}

}
